package com.service;

import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.Handler;
import android.os.IBinder;
import com.dao.Joke;
import com.dao.JokeFactory;
import com.enums.JokeName;
import com.enums.JokeState;


/* Gerer le cycle de vie des jokes */
public class JokeManager extends Service{

	public static Boolean isServiceStarted = false;	
	public static JokeFactory jokesFactory = new JokeFactory();
	public static Handler serviceHandler = new Handler();
	
	Joke joke = null;

	
	@Override
	public void onCreate() {
	}

	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		
		joke  = jokesFactory.getJoke(JokeName.JOKE1);
//		joke  = joke.getJoke_state() == JokeState.RUNNABLE ? joke : jokesFactory.getNextJoke(joke);
		
		serviceHandler.postDelayed(joke, 1000L);
		
		return START_STICKY;
	}

	
	@Override
	public void onDestroy() {

		joke.stop();
		serviceHandler.removeCallbacksAndMessages(joke);
	}

	@Override
	public boolean onUnbind(Intent intent) {

		return super.onUnbind(intent);
	}

	private final IBinder mBinder = new MonServiceBinder();

	@Override
	public IBinder onBind(Intent intent) {
		
		return mBinder;
	}

	public class MonServiceBinder extends Binder {
		public JokeManager getService() {
			return JokeManager.this;
		}
	}

}
